package com.mygdx.game.desktop.demo;

import java.util.ArrayList;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonValue;

/**
 * 对话框测试类
 * @author Administrator
 *
 */
public class DialogTestGame extends Game {

	Stage stage;
	Skin skin;
	Window window;
	Table table;
	TextButton goingButton;
	TextButton gobackButton;
	Label speakerLabel;
	Label dialogLabel;
	
	Image imageJiang;
	Image imageHan;
	
	float width, height;
	static final float ratioW = 0.9f;
	static final float ratioH = 4f;
	Json json = new Json();
	ArrayList<JsonValue> dialogslist;
	int currentPage = 0;
	@Override
	public void create() {
		// 读取对话json文件
		dialogslist = json.fromJson(ArrayList.class, Gdx.files.internal("dialog/testDialog.json"));
		// 舞台
		stage = new Stage();
		//stage.setDebugAll(true);
		skin = new Skin(Gdx.files.internal("skins/uiskin.json"));
		// 接收输入事件
		Gdx.input.setInputProcessor(stage);
		width = stage.getWidth();
		height = stage.getHeight();
		
		// 对话框
		window = new Window("Views", skin);
		// 设置window的长宽根据stage的长宽
		window.setWidth(width*ratioW);
		window.setHeight(height/ratioH);
		// 设置位置(居中，距底10px)
		window.setPosition(width*((1-ratioW)/2), 10f);
		
		// 对话框格局
		table = new Table();
		table.setWidth(width*ratioW);
		table.setHeight(height/ratioH);
		// 前进按钮
		goingButton = new TextButton("going", skin);
		// 后退按钮
		gobackButton = new TextButton("goback", skin);
		// 对话框
		speakerLabel = new Label(dialogslist.get(currentPage).getString("speaker")+":", skin);
		dialogLabel = new Label(dialogslist.get(currentPage).getString("content"), skin);
		table.add(speakerLabel).left().expandX();
		table.add();
		table.row();
		table.add(dialogLabel).left().expandX().padLeft(20);
		table.add(goingButton).fillX().padRight(20);
		table.row();
		table.add();
		table.add(gobackButton).padRight(20);
		
		// 人物图像
		imageJiang = new Image(new TextureRegion(new Texture(Gdx.files.internal("images/hero/marisa.png")))); 
		setScale(imageJiang, height);
		imageJiang.addAction(Actions.moveTo(10f, 0, 0.3f, Interpolation.swingOut));
		TextureRegion aliceTR = new TextureRegion(new Texture(Gdx.files.internal("images/hero/alice.png")));
		aliceTR.flip(true, false);	// x轴翻转
		imageHan = new Image(aliceTR);
		setScale(imageHan, height);
		// 设置靠右
		imageHan.addAction(Actions.moveTo(width/2, 0, 0.3f, Interpolation.swingOut));
		
		
		window.addActor(table);
		// 舞台添加演员
		stage.addActor(window);
		
		goingButton.addListener(new ChangeListener() {
	    	@Override
	        public void changed(ChangeEvent event, Actor actor) {
	            if(currentPage<dialogslist.size()-1) {
	            	currentPage++;
	            }
	            System.out.println("currentPage:"+currentPage);
	        }
		});
		
		gobackButton.addListener(new ChangeListener() {
	    	@Override
	        public void changed(ChangeEvent event, Actor actor) {
	    		if(currentPage>0) {
	    			currentPage--;
	    		}
	    		System.out.println("currentPage:"+currentPage);
	        }
		});
		
	}
	
	@Override
    public void render() {
        Gdx.gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        dialogSwitcher();
        stage.act();
        stage.draw();
    }

	@Override
    public void dispose() {
        stage.dispose();
        skin.dispose();
    }

    @Override
    public void resize(int width, int height) {
        stage.getViewport().update(width, height);
    }
	
    /**
     * .对话切换
     */
    private void dialogSwitcher() {
    	String speaker = dialogslist.get(currentPage).getString("speaker");
    	switch (speaker) {
    		case "han": 
    			stage.addActor(imageHan);
    			setZIndex();
    			break;
    		case "jiang":
    			stage.addActor(imageJiang);
    			setZIndex();
    			break;
    	}
    	
    	speakerLabel.setText(speaker+":");
        dialogLabel.setText(dialogslist.get(currentPage).getString("content"));
    }
    
    /**
     * .设置actor的堆叠顺序
     * @param 、args
     */
    private void setZIndex(){
    	window.setZIndex(stage.getActors().size-1);
    }
    
    /**
     * .按比例缩放
     * @param 、actor
     * @param 、height
     */
    private void setScale(Actor actor, float originalHeight) {
    	actor.setWidth(actor.getWidth()*(originalHeight/actor.getHeight()));
    	actor.setHeight(originalHeight);
    }
    
	public static void main(String[] args) {
		new LwjglApplication(new DialogTestGame());
	}
}